package com.zkld.frame.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Aspect
@Component
public class UserAspect {

    private Logger logger = LoggerFactory.getLogger(UserAspect.class);

    @Pointcut("execution(* com.zkld.frame.service.impl.UserServiceImpl.login(..))")
    public void pointerCu() {
    }

    @Before("pointerCu()")
    public void before(JoinPoint joinPoint) {
        Integer userId = (Integer) joinPoint.getArgs()[0];
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        logger.info(userId + "登录，时间为:" + df.format(new Date()));
    }

    @AfterReturning(pointcut = "pointerCu()", returning = "res")
    public void after(JoinPoint joinPoint, Object res) {
        logger.info("方法:" + joinPoint.getSignature().getName());
    }

    @AfterThrowing(pointcut = "pointerCu()", throwing = "a")
    public void exceptionMessage(Throwable a) {
        logger.error("出现异常:" + a.getMessage() + "cause:" + a.getCause());
        System.out.println("-------------method error---------------------");
    }

}
